<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function _default(){
    global $username,$passwd;
$password=  get_user_info($username, "passwd");
if (strcmp($passwd,$password)==0){
   $_SESSION['is_login']=TRUE;
   $_SESSION['user_name']=$username;
   if (strcmp($username, "admin")){
       $_SESSION['is_admin']=TRUE;
   } else {
       $_SESSION['is_admin']=FALSE;
   }
   echo "Login success";
} else {
    echo "Login fail";
}
}
function _profile_frame(){
    global $is_login,$user_name;
    $fullname=  get_user_info($user_name, "fullname");
    echo "<div id=\"profile_frame\">
             <table><tr><td>
         <div id=\"active_user\"><b>$fullname</b></div>
                    </td><td>
         <div id=\"notification\">0</div></td>
         <td>
         <img id=\"user_profile\" alt=\"User profile\" class=\"icon_profile\" src=\"img/man05.png\"/></td>
                     <td><img alt=\"Show Menu\" src=\"img/downarrow.gif\"></td>
                     <td>&nbsp;</td></tr></table>
         </div>";
    ?>
<script>
    $("#profile_frame").click(function(){
        var pos=$("#user_profile").position();
        var x=pos.left;
        var y=pos.top;
        var height=$(this).outerHeight();
        
        $("#profile_block").css({
        position:"absolute",
        top : (y + height + 14 ) +"px",
        left : (x - 240) +"px"
        }).toggle();        
        });
        
    $("#button_logout").click(function(){
        $("#user_block").load('core.php?mod=users&file=login&action=logout');
        $("#profile_block").hide();
    });  
</script>
<?php
}
function _login_frame(){
    echo "<div id=\"login_frame\"><a href=\"#\">Sign in</a></div>";
    ?>
<script>
        $("#login_frame").click(function(){
        var pos=$(this).position();
        var x=pos.left;
        var y=pos.top;
        var height=$(this).outerHeight();
        
        $("#login_block").css({
        position:"absolute",
        top : (y + height + 14 ) +"px",
        left : (x - 240) +"px"
        }).toggle();  
        });
        
        $("#button_login").click(function(){
            var str=$("#login_form").serialize();
            var vurl="core.php?mod=users&file=login&action=login";
            $.ajax({
                type:"POST",
                url:vurl,
                data : str, success:function(msg){
                    $("#user_block").load('core.php?mod=users&file=login&action=load');
                    $("#login_frame").hide();
                    $("#login_block").hide();
                    //alert(msg);
                    $("#content").text(msg);
                }
            });
           $("#login_block").hide(); 
        });
        </script>
<?php
}
function _load(){
    global $is_login,$user_name;
    if (isset($is_login)&&$is_login==TRUE){
        _profile_frame();
    } else {
        _login_frame();
    }
}
global $action;
switch ($action){
    case 'logout':
        $_SESSION['is_login']=FALSE;
        $_SESSION['user_name']='';
   
        $_SESSION['is_admin']=FALSE;
   
        _login_frame();
        break;
    case 'load':
        _load();
        break;
    case 'login':
    default:
        _default();
}
?>

